<?php

namespace app\admin\controller\site;

use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\site\Index
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\site\Index;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function info()
    {
        if($this->request->isAjax()) {
            // 查询是否已存在数据
            $index_name = input('row.index_name');
            $this->setConfig($index_name ,'index_name' ,'index' ,'请填写网站名称');

            $gongan = input('row.index_beian');
            $this->setConfig($gongan ,'index_beian' ,'index' ,'请填写备案号');

            $copyright = input('row.index_gongan');
            $this->setConfig($copyright ,'index_gongan' ,'index' ,'请填写公安备案号');

            $support = input('row.index_copyright');
            $this->setConfig($support ,'index_copyright' ,'index' ,'请填写版权信息');

            $gongzhong = input('row.gongzhong_img');
            $this->setConfig($gongzhong ,'gongzhong_img' ,'index');

            $mobile = input('row.mobile_img');
            $this->setConfig($mobile ,'mobile_img' ,'index');

            $intro = input('row.zjmt_intro');
            $this->setConfig($intro ,'zjmt_intro' ,'index');

            $history = input('row.zjmt_history');
            $this->setConfig($history ,'zjmt_history' ,'index');

            $zjmt_bz_num = input('row.zjmt_bz_num');
            $this->setConfig($zjmt_bz_num ,'zjmt_bz_num' ,'index');

            $zjmt_js_num = input('row.zjmt_js_num');
            $this->setConfig($zjmt_js_num ,'zjmt_js_num' ,'index');

            $zjmt_mj_num = input('row.zjmt_mj_num');
            $this->setConfig($zjmt_mj_num ,'zjmt_mj_num' ,'index');

            $zjmt_jd_num = input('row.zjmt_jd_num');
            $this->setConfig($zjmt_jd_num ,'zjmt_jd_num' ,'index');

            $this->success();
        }

        $data = [
            'index_name'        => ['value' => '' ,'tip' => ''],
            'index_beian'       => ['value' => '' ,'tip' => ''],
            'index_gongan'      => ['value' => '' ,'tip' => ''],
            'index_copyright'   => ['value' => '' ,'tip' => ''],
            'index_support'     => ['value' => '' ,'tip' => ''],
            'zjmt_intro'        => ['value' => '' ,'tip' => ''],
            'zjmt_history'      => ['value' => '' ,'tip' => ''],
            'zjmt_bz_num'       => ['value' => '' ,'tip' => ''],
            'zjmt_js_num'       => ['value' => '' ,'tip' => ''],
            'zjmt_mj_num'       => ['value' => '' ,'tip' => ''],
            'zjmt_jd_num'       => ['value' => '' ,'tip' => ''],
            'gongzhong_img'     => ['value' => '/assets/img/gongzhong_qrcode.png' ,'tip' => ''],
            'mobile_img'        => ['value' => '/assets/img/mobile_qrcode.png' ,'tip' => ''],
        ];
        $index = ConfigModel::where('group', 'index')->select();
        if($index){
            foreach ($index as $item){
                $data[$item['name']] = [
                    'value' => $item['value'],
                    'tip' => $item['tip'],
                ];
            }

        }
        $this->view->assign('data', $data);
        return $this->fetch('info');
    }

    public function setConfig($value ,$name ,$group ,$tip='' ,$type='string')
    {
        $model = ConfigModel::where('name', $name)->where('group', $group)->find();
        if (!$model) {
            $model = new ConfigModel;
        }
        $model->name = $name;
        $model->group = $group;
        $model->title = ucfirst($name);
        $model->type = $type;
        $model->tip = $tip;
        $model->value = $value;
        $model->save();
    }
}
